# import sys
# import threading
# import time
# import rclpy
# from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
# from PyQt5.QtCore import QObject, pyqtSignal, QTimer
# from sensor_msgs.msg import Image 
# from cv_bridge import CvBridge
# from rclpy.node import Node
# from ui.show_ui import *
# import cv2
# import numpy as np
# from yolo_msg.msg import Yolo
# from std_msgs.msg import String


# class RclCommNode(Node, QMainWindow, Ui_MainWindow):
#     def __init__(self):
#         Node.__init__(self, "show")
#         QMainWindow.__init__(self)
#         self.setupUi(self)  # 设置UI
#         self.camera_sub0 = self.create_subscription(Image, '/img0', self.camera0_callback, 10)
#         self.camera_sub1 = self.create_subscription(Image, '/img1', self.camera1_callback, 10)
#         self.camera_sub = self.create_subscription(Image, '/img_drone', self.camera_drone_callback, 10)
#         self.yolo_sub = self.create_subscription(Yolo, '/face', self.face_callback, 10)
#         self.control_pub = self.create_publisher(String, '/control', 10)
#         self.timer = self.create_timer(0.1, self.timer_callback)

#         self.Button_help0.clicked.connect(self.onButton_help0)
#         self.Button_help1.clicked.connect(self.onButton_help1)
#         self.Button_ignore.clicked.connect(self.onButton_ignore)



        

#         self.control_command = "0"
#         self.state = 0

#         self.count = 0
#         self.camid = 0


#     def onButton_help0(self):
#         if self.state == 1:
#             if self.camid == 0:
#                 for i in range(10):
#                     String_msg = String()
#                     String_msg.data = '10'
#                     self.control_pub.publish(String_msg)
#                 print("发布指令10")
#             if self.camid == 1:
#                 for i in range(10):
#                     String_msg = String()
#                     String_msg.data = '11'
#                     self.control_pub.publish(String_msg)
#                 print("发布指令11")
#         else:
#             print("未发现落水人员")
        
#     def onButton_help1(self):
#         if self.state == 1:
#             for i in range(10):
#                 String_msg = String()
#                 String_msg.data = '2'
#                 self.control_pub.publish(String_msg)
#             print("发布指令2")
#         else:
#             print("未发现落水人员")
    
#     def onButton_ignore(self):
#         if self.state ==1:
#             print("忽略")
#             self.state = 0
#         else:
#             print("未发现落水人员")

#     def camera0_callback(self, img):
#         self.show_cv_img(img, 0)
#     def camera1_callback(self, img):
#         self.show_cv_img(img, 1)
#     def camera_drone_callback(self, img):
#         self.show_cv_img(img, 3)

#     def show_cv_img(self, img, id):
#         bridge = CvBridge()
#         img = bridge.imgmsg_to_cv2(img, 'bgr8')
#         shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#         QtImg = QtGui.QImage(shrink.data,
#                              shrink.shape[1],
#                              shrink.shape[0],
#                              shrink.shape[1] * 3,
#                              QtGui.QImage.Format_RGB888)
#         if id == 0:
#             jpg_out = QtGui.QPixmap(QtImg).scaled(
#                 self.imgshow0.width(), self.imgshow0.height()) 
#             self.imgshow0.setPixmap(jpg_out)
#         elif id == 1:
#             jpg_out = QtGui.QPixmap(QtImg).scaled(
#                 self.imgshow1.width(), self.imgshow1.height()) 
#             self.imgshow1.setPixmap(jpg_out)
#         elif id == 3:
#             jpg_out = QtGui.QPixmap(QtImg).scaled(
#                 self.imgshowdrone.width(), self.imgshow1.height()) 
#             self.imgshowdrone.setPixmap(jpg_out)
#     def face_callback(self, msg):
#         _translate = QtCore.QCoreApplication.translate
#         self.state = 1
#         self.label.setText(_translate("MainWindow", "有人落水"))
#         self.camid = msg.camid

#     def timer_callback(self):
#         # self.count += 1
#         # if self.count >= 600:
#         _translate = QtCore.QCoreApplication.translate
#         if self.state == 0:
#             self.label.setText(_translate("MainWindow", "无人落水"))
#         pass

        

        
# def spin_thread(node):
#     rclpy.spin(node)

# def main(args=None):
#     app = QApplication(sys.argv) 

#     rclpy.init(args=args)
#     node = RclCommNode() 
#     node.show()
#     threading.Thread(target=spin_thread, args=(node,), daemon=True).start()

#     sys.exit(app.exec_())

# import sys
# import threading
# import time
# import rclpy
# from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
# from PyQt5.QtCore import QObject, pyqtSignal, QTimer, Qt, QThread
# from PyQt5 import QtGui  # 添加导入 QtGui
# from sensor_msgs.msg import Image
# from cv_bridge import CvBridge
# from rclpy.node import Node
# from ui.show_ui import *
# import cv2
# import numpy as np
# from yolo_msg.msg import Yolo
# from std_msgs.msg import String


# class RclCommNode(Node, QMainWindow, Ui_MainWindow):
#     def __init__(self):
#         Node.__init__(self, "show")
#         QMainWindow.__init__(self)
#         self.setupUi(self)
#         self.camera_sub0 = self.create_subscription(Image, '/img0', self.camera0_callback, 10)
#         self.camera_sub1 = self.create_subscription(Image, '/img1', self.camera1_callback, 10)
#         self.camera_drone_sub = self.create_subscription(Image, '/img_drone', self.camera_drone_callback, 10)  # 修改订阅器名称
#         self.yolo_sub = self.create_subscription(Yolo, '/face', self.face_callback, 10)
#         self.control_pub = self.create_publisher(String, '/control', 10)
#         self.timer = self.create_timer(0.1, self.timer_callback)

#         self.Button_help0.clicked.connect(self.onButton_help0)
#         self.Button_help1.clicked.connect(self.onButton_help1)
#         self.Button_ignore.clicked.connect(self.onButton_ignore)

#         self.control_command = "0"
#         self.state = 0

#         self.count = 0
#         self.camid = 0

#     def onButton_help0(self):
#         if self.state == 1:
#             if self.camid == 0:
#                 for i in range(10):
#                     String_msg = String()
#                     String_msg.data = '10'
#                     self.control_pub.publish(String_msg)
#                 print("发布指令10")
#             if self.camid == 1:
#                 for i in range(10):
#                     String_msg = String()
#                     String_msg.data = '11'
#                     self.control_pub.publish(String_msg)
#                 print("发布指令11")
#         else:
#             print("未发现落水人员")
        
#     def onButton_help1(self):
#         if self.state == 1:
#             for i in range(10):
#                 String_msg = String()
#                 String_msg.data = '2'
#                 self.control_pub.publish(String_msg)
#             print("发布指令2")
#         else:
#             print("未发现落水人员")
    
#     def onButton_ignore(self):
#         if self.state == 1:
#             print("忽略")
#             self.state = 0
#         else:
#             print("未发现落水人员")

#     def camera0_callback(self, img):
#         self.show_cv_img(img, 0)

#     def camera1_callback(self, img):
#         self.show_cv_img(img, 1)

#     def camera_drone_callback(self, img):
#         self.show_cv_img(img, 3)

#     def show_cv_img(self, img, id):
#         bridge = CvBridge()
#         img = bridge.imgmsg_to_cv2(img, 'bgr8')
#         shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#         QtImg = QtGui.QImage(shrink.data,
#                              shrink.shape[1],
#                              shrink.shape[0],
#                              shrink.shape[1] * 3,
#                              QtGui.QImage.Format_RGB888)
#         if id == 0:
#             jpg_out = QtGui.QPixmap(QtImg).scaled(self.imgshow0.width(), self.imgshow0.height())
#             self.imgshow0.setPixmap(jpg_out)
#         elif id == 1:
#             jpg_out = QtGui.QPixmap(QtImg).scaled(self.imgshow1.width(), self.imgshow1.height())
#             self.imgshow1.setPixmap(jpg_out)
#         elif id == 3:
#             jpg_out = QtGui.QPixmap(QtImg).scaled(self.imgshowdrone.width(), self.imgshowdrone.height())  # 修改尺寸
#             self.imgshowdrone.setPixmap(jpg_out)

#     def face_callback(self, msg):
#         _translate = QtCore.QCoreApplication.translate
#         self.state = 1
#         self.label.setText(_translate("MainWindow", "有人落水"))
#         self.camid = msg.camid

#     def timer_callback(self):
#         _translate = QtCore.QCoreApplication.translate
#         if self.state == 0:
#             self.label.setText(_translate("MainWindow", "无人落水"))
#         pass


# class SpinThread(QThread):
#     def __init__(self, node):
#         super().__init__()
#         self.node = node

#     def run(self):
#         rclpy.spin(self.node)


# def main(args=None):
#     app = QApplication(sys.argv)

#     rclpy.init(args=args)
#     node = RclCommNode()
#     node.show()

#     # 使用 QThread 启动 ROS spin
#     spin_thread = SpinThread(node)
#     spin_thread.start()

#     sys.exit(app.exec_())
#     rclpy.shutdown()


# if __name__ == '__main__':
#     main()




import sys
import rclpy
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import QThread
from PyQt5 import QtGui
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
from rclpy.node import Node
from ui.show_ui import *
import cv2
import numpy as np
from yolo_msg.msg import Yolo
from std_msgs.msg import String


class RclCommNode(Node, QMainWindow, Ui_MainWindow):
    def __init__(self):
        Node.__init__(self, "show")
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.camera_sub0 = self.create_subscription(Image, '/img0', self.camera0_callback, 10)
        self.camera_sub1 = self.create_subscription(Image, '/img1', self.camera1_callback, 10)
        self.camera_drone_sub = self.create_subscription(Image, '/img_drone', self.camera_drone_callback, 10)
        self.yolo_sub = self.create_subscription(Yolo, '/face', self.face_callback, 10)
        self.control_pub = self.create_publisher(String, '/control', 10)
        self.timer = self.create_timer(0.1, self.timer_callback)

        self.Button_help0.clicked.connect(self.onButton_help0)
        self.Button_help1.clicked.connect(self.onButton_help1)
        self.Button_ignore.clicked.connect(self.onButton_ignore)

        self.control_command = "0"
        self.state = 0
        self.count = 0
        self.camid = 0

    def onButton_help0(self):
        if self.state == 1:
            if self.camid == 0:
                for i in range(10):
                    String_msg = String()
                    String_msg.data = '20'
                    self.control_pub.publish(String_msg)
                print("发布指令20")
            if self.camid == 1:
                for i in range(10):
                    String_msg = String()
                    String_msg.data = '21'
                    self.control_pub.publish(String_msg)
                print("发布指令21")
        else:
            print("未发现落水人员")
        
    def onButton_help1(self):
        if self.state == 1:
            if self.camid == 0:
                for i in range(10):
                    String_msg = String()
                    String_msg.data = '10'
                    self.control_pub.publish(String_msg)
                print("发布指令10")
            if self.camid == 1:
                for i in range(10):
                    String_msg = String()
                    String_msg.data = '11'
                    self.control_pub.publish(String_msg)
                print("发布指令11")
        else:
            print("未发现落水人员")
    
    def onButton_ignore(self):
        if self.state == 1:
            print("忽略")
            self.state = 0
        else:
            print("未发现落水人员")

    def camera0_callback(self, img):
        self.show_cv_img(img, 0)

    def camera1_callback(self, img):
        self.show_cv_img(img, 1)

    def camera_drone_callback(self, img):
        self.show_cv_img(img, 3)

    def show_cv_img(self, img, id):
        bridge = CvBridge()
        img = bridge.imgmsg_to_cv2(img, 'bgr8')
        shrink = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        QtImg = QtGui.QImage(shrink.data,
                             shrink.shape[1],
                             shrink.shape[0],
                             shrink.shape[1] * 3,
                             QtGui.QImage.Format_RGB888)
        if id == 0:
            jpg_out = QtGui.QPixmap(QtImg).scaled(self.imgshow0.width(), self.imgshow0.height())
            self.imgshow0.setPixmap(jpg_out)
        elif id == 1:
            jpg_out = QtGui.QPixmap(QtImg).scaled(self.imgshow1.width(), self.imgshow1.height())
            self.imgshow1.setPixmap(jpg_out)
        elif id == 3:
            jpg_out = QtGui.QPixmap(QtImg).scaled(self.imgshowdrone.width(), self.imgshowdrone.height())
            self.imgshowdrone.setPixmap(jpg_out)

    def face_callback(self, msg):
        _translate = QtCore.QCoreApplication.translate
        self.state = 1
        self.label.setText(_translate("MainWindow", "有人落水"))
        self.camid = msg.camid

    def timer_callback(self):
        _translate = QtCore.QCoreApplication.translate
        if self.state == 0:
            self.label.setText(_translate("MainWindow", "无人落水"))
        pass


class SpinThread(QThread):
    def __init__(self, node):
        super().__init__()
        self.node = node

    def run(self):
        rclpy.spin(self.node)  # 在该线程中运行 ROS spin


def main(args=None):
    app = QApplication(sys.argv)

    rclpy.init(args=args)
    node = RclCommNode()
    node.show()

    # 创建 SpinThread 来处理 ROS 的通信
    spin_thread = SpinThread(node)
    spin_thread.start()  # 在独立线程中启动 ROS spin

    sys.exit(app.exec_())  # 启动 Qt 事件循环
    rclpy.shutdown()  # 在应用关闭后关闭 ROS


if __name__ == '__main__':
    main()
